// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

/**
 * Created by eran on 15-1-5.
 */
package gui.component.mapEditor.block
{
    import data.BlockVo;
    import data.DataVoManger;

    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;

    public class MapBlockLayer
    {
        private var mContainer:Sprite;

        private var mAllBlockTileRenderVector:Vector.<BlockTileRender>;
        private var mTempSwapVector:Vector.<BlockTileRender>;

        public function MapBlockLayer()
        {
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        public function initialize():void
        {
            mContainer = new Sprite();
            mAllBlockTileRenderVector = new Vector.<BlockTileRender>();
            mTempSwapVector = new Vector.<BlockTileRender>();
        }

        //=================================//
        //====== Public Function ==========//
        //=================================//

        public function refresh():void
        {
            //=================
            //1`保留已有的Render
            //=================
            for each(var vo:BlockVo in DataVoManger.instance.allBlockVoArray)
            {
                var blockRender:BlockTileRender = findAndRemoveBlockRenderFromVector(vo);
                if (blockRender == null)
                {
                    blockRender = new BlockTileRender();
                    blockRender.initialize(vo);
                    mContainer.addChild(blockRender.container);
                }
                mTempSwapVector.push(blockRender);
            }

            //=================
            //2`删除无用的Render
            //=================
            while (mAllBlockTileRenderVector.length > 0)
            {
                var unUseBlockRender:BlockTileRender = mAllBlockTileRenderVector.pop();
                mContainer.removeChild(unUseBlockRender.container);
                unUseBlockRender.dispose();
            }

            //================
            //3`交换Vector
            //================
            var swapVar:Vector.<BlockTileRender> = mAllBlockTileRenderVector;
            mAllBlockTileRenderVector = mTempSwapVector;
            mTempSwapVector = swapVar;

            //==================
            //4` 刷新所有的Render
            //==================
            doRefreshAllBlockRender();
        }

        public function get container():DisplayObjectContainer {return mContainer;}

        //=================================//
        //====== Private Function =========//
        //=================================//

        private function doRefreshAllBlockRender():void
        {
            for each(var render:BlockTileRender in mAllBlockTileRenderVector)
            {
                render.refresh();
            }
        }

        private function findAndRemoveBlockRenderFromVector(_vo:BlockVo):BlockTileRender
        {
            for (var i:int = 0; i < mAllBlockTileRenderVector.length; i++)
            {
                var render:BlockTileRender = mAllBlockTileRenderVector[i];
                if (render.vo == _vo)
                {
                    mAllBlockTileRenderVector.splice(i, 1);
                    return render;
                }
            }
            return null;
        }

    }
}
